package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import com.tanhua.server.vo.TodayBest;
import org.springframework.stereotype.Service;

/**
 * 与dubbo交互
 */
@Service
public class RecommendUserService {

    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    /**
     * 查询得分最高用户
     *
     * @param userId
     * @return
     */
    public TodayBest queryTodayBest(Long userId) {
        //查询一位得分最高的推荐用户
        RecommendUser recommendUser = this.recommendUserApi.queryWithMaxScore(userId);
        //如果没有，直接返回null
        if (recommendUser == null) {
            return null;
        }

        //设置资料
        TodayBest todayBest = new TodayBest();
        todayBest.setId(recommendUser.getUserId());
        //获取缘分值，直接取整
        double score = Math.floor(recommendUser.getScore());
        todayBest.setFateValue(Double.valueOf(score).longValue());
        return todayBest;
    }

    /**
     * 分页查询所有推荐用户
     *
     * @param userId
     */
    public PageInfo<RecommendUser> queryRecommendUserList(Long userId, Integer page, Integer pageSize) {
        return this.recommendUserApi.queryPageInfo(userId, page, pageSize);
    }

    /**
     * 查询推荐好友的缘分值
     *
     * @param userId
     * @param toUserId
     * @return
     */
    public Double queryScore(Long userId, Long toUserId){
        //查询缘分值
        Double score = this.recommendUserApi.queryScore(userId, toUserId);
        if (score != null){
            return score;
        }

        //如果为空直接返回默认值
        return 80d;

    }
}
